{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "8H17bBI3N-vo"
      },
      "source": [
        "\n",
        "\n",
        "## RapidOCR Python Demo\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "0qapeJlG6JFJ"
      },
      "source": [
        "#### Install rapidocr"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "sTB1PsMp6XTT",
        "outputId": "47d2f7d9-6c5f-4269-d872-3b9bfa385161"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Collecting rapidocr\n",
            "  Downloading rapidocr-3.2.0-py3-none-any.whl.metadata (1.2 kB)\n",
            "Collecting onnxruntime\n",
            "  Downloading onnxruntime-1.22.0-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.metadata (4.5 kB)\n",
            "Collecting pyclipper>=1.2.0 (from rapidocr)\n",
            "  Downloading pyclipper-1.3.0.post6-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (9.0 kB)\n",
            "Requirement already satisfied: opencv-python>=4.5.1.48 in /usr/local/lib/python3.11/dist-packages (from rapidocr) (4.11.0.86)\n",
            "Requirement already satisfied: numpy<3.0.0,>=1.19.5 in /usr/local/lib/python3.11/dist-packages (from rapidocr) (2.0.2)\n",
            "Requirement already satisfied: six>=1.15.0 in /usr/local/lib/python3.11/dist-packages (from rapidocr) (1.17.0)\n",
            "Requirement already satisfied: Shapely!=2.0.4,>=1.7.1 in /usr/local/lib/python3.11/dist-packages (from rapidocr) (2.1.1)\n",
            "Requirement already satisfied: PyYAML in /usr/local/lib/python3.11/dist-packages (from rapidocr) (6.0.2)\n",
            "Requirement already satisfied: Pillow in /usr/local/lib/python3.11/dist-packages (from rapidocr) (11.2.1)\n",
            "Requirement already satisfied: tqdm in /usr/local/lib/python3.11/dist-packages (from rapidocr) (4.67.1)\n",
            "Requirement already satisfied: omegaconf in /usr/local/lib/python3.11/dist-packages (from rapidocr) (2.3.0)\n",
            "Requirement already satisfied: requests in /usr/local/lib/python3.11/dist-packages (from rapidocr) (2.32.3)\n",
            "Collecting colorlog (from rapidocr)\n",
            "  Downloading colorlog-6.9.0-py3-none-any.whl.metadata (10 kB)\n",
            "Collecting coloredlogs (from onnxruntime)\n",
            "  Downloading coloredlogs-15.0.1-py2.py3-none-any.whl.metadata (12 kB)\n",
            "Requirement already satisfied: flatbuffers in /usr/local/lib/python3.11/dist-packages (from onnxruntime) (25.2.10)\n",
            "Requirement already satisfied: packaging in /usr/local/lib/python3.11/dist-packages (from onnxruntime) (24.2)\n",
            "Requirement already satisfied: protobuf in /usr/local/lib/python3.11/dist-packages (from onnxruntime) (5.29.5)\n",
            "Requirement already satisfied: sympy in /usr/local/lib/python3.11/dist-packages (from onnxruntime) (1.13.1)\n",
            "Collecting humanfriendly>=9.1 (from coloredlogs->onnxruntime)\n",
            "  Downloading humanfriendly-10.0-py2.py3-none-any.whl.metadata (9.2 kB)\n",
            "Requirement already satisfied: antlr4-python3-runtime==4.9.* in /usr/local/lib/python3.11/dist-packages (from omegaconf->rapidocr) (4.9.3)\n",
            "Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.11/dist-packages (from requests->rapidocr) (3.4.2)\n",
            "Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.11/dist-packages (from requests->rapidocr) (3.10)\n",
            "Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.11/dist-packages (from requests->rapidocr) (2.4.0)\n",
            "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.11/dist-packages (from requests->rapidocr) (2025.6.15)\n",
            "Requirement already satisfied: mpmath<1.4,>=1.1.0 in /usr/local/lib/python3.11/dist-packages (from sympy->onnxruntime) (1.3.0)\n",
            "Downloading rapidocr-3.2.0-py3-none-any.whl (15.0 MB)\n",
            "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m15.0/15.0 MB\u001b[0m \u001b[31m57.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25hDownloading onnxruntime-1.22.0-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (16.4 MB)\n",
            "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m16.4/16.4 MB\u001b[0m \u001b[31m50.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25hDownloading pyclipper-1.3.0.post6-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (969 kB)\n",
            "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m969.6/969.6 kB\u001b[0m \u001b[31m36.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25hDownloading coloredlogs-15.0.1-py2.py3-none-any.whl (46 kB)\n",
            "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m46.0/46.0 kB\u001b[0m \u001b[31m4.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25hDownloading colorlog-6.9.0-py3-none-any.whl (11 kB)\n",
            "Downloading humanfriendly-10.0-py2.py3-none-any.whl (86 kB)\n",
            "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m86.8/86.8 kB\u001b[0m \u001b[31m4.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25hInstalling collected packages: pyclipper, humanfriendly, colorlog, rapidocr, coloredlogs, onnxruntime\n",
            "Successfully installed coloredlogs-15.0.1 colorlog-6.9.0 humanfriendly-10.0 onnxruntime-1.22.0 pyclipper-1.3.0.post6 rapidocr-3.2.0\n"
          ]
        }
      ],
      "source": [
        "!pip install rapidocr onnxruntime"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "PDlCLO_CewSk"
      },
      "source": [
        "#### Run"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 1000
        },
        "id": "bbHGS0emSNTz",
        "outputId": "c75433e9-e5b9-4679-d823-88221a0be488"
      },
      "outputs": [
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "\u001b[32m[INFO] 2025-06-19 13:01:55,418 [RapidOCR] base.py:24: Using engine_name: onnxruntime\u001b[0m\n",
            "\u001b[32m[INFO] 2025-06-19 13:01:55,466 [RapidOCR] download_file.py:56: File exists and is valid: /usr/local/lib/python3.11/dist-packages/rapidocr/models/ch_PP-OCRv4_det_infer.onnx\u001b[0m\n",
            "\u001b[32m[INFO] 2025-06-19 13:01:55,467 [RapidOCR] main.py:55: Using /usr/local/lib/python3.11/dist-packages/rapidocr/models/ch_PP-OCRv4_det_infer.onnx\u001b[0m\n",
            "\u001b[32m[INFO] 2025-06-19 13:01:55,562 [RapidOCR] base.py:24: Using engine_name: onnxruntime\u001b[0m\n",
            "\u001b[32m[INFO] 2025-06-19 13:01:55,568 [RapidOCR] download_file.py:56: File exists and is valid: /usr/local/lib/python3.11/dist-packages/rapidocr/models/ch_ppocr_mobile_v2.0_cls_infer.onnx\u001b[0m\n",
            "\u001b[32m[INFO] 2025-06-19 13:01:55,570 [RapidOCR] main.py:55: Using /usr/local/lib/python3.11/dist-packages/rapidocr/models/ch_ppocr_mobile_v2.0_cls_infer.onnx\u001b[0m\n",
            "\u001b[32m[INFO] 2025-06-19 13:01:55,617 [RapidOCR] base.py:24: Using engine_name: onnxruntime\u001b[0m\n",
            "\u001b[32m[INFO] 2025-06-19 13:01:55,653 [RapidOCR] download_file.py:56: File exists and is valid: /usr/local/lib/python3.11/dist-packages/rapidocr/models/ch_PP-OCRv4_rec_infer.onnx\u001b[0m\n",
            "\u001b[32m[INFO] 2025-06-19 13:01:55,655 [RapidOCR] main.py:55: Using /usr/local/lib/python3.11/dist-packages/rapidocr/models/ch_PP-OCRv4_rec_infer.onnx\u001b[0m\n",
            "\u001b[32m[INFO] 2025-06-19 13:01:58,410 [RapidOCR] download_file.py:64: Initiating download: https://www.modelscope.cn/models/RapidAI/RapidOCR/resolve/v3.1.0/resources/fonts/FZYTK.TTF\u001b[0m\n"
          ]
        },
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "RapidOCROutput(img=array([[[255, 255, 255],\n",
            "        [255, 255, 255],\n",
            "        [255, 255, 255],\n",
            "        ...,\n",
            "        [255, 255, 255],\n",
            "        [255, 255, 255],\n",
            "        [255, 255, 255]],\n",
            "\n",
            "       [[255, 255, 255],\n",
            "        [255, 255, 255],\n",
            "        [255, 255, 255],\n",
            "        ...,\n",
            "        [255, 255, 255],\n",
            "        [255, 255, 255],\n",
            "        [255, 255, 255]],\n",
            "\n",
            "       [[255, 255, 255],\n",
            "        [255, 255, 255],\n",
            "        [255, 255, 255],\n",
            "        ...,\n",
            "        [255, 255, 255],\n",
            "        [255, 255, 255],\n",
            "        [255, 255, 255]],\n",
            "\n",
            "       ...,\n",
            "\n",
            "       [[255, 255, 255],\n",
            "        [255, 255, 255],\n",
            "        [255, 255, 255],\n",
            "        ...,\n",
            "        [254, 254, 254],\n",
            "        [254, 254, 254],\n",
            "        [254, 254, 254]],\n",
            "\n",
            "       [[255, 255, 255],\n",
            "        [255, 255, 255],\n",
            "        [255, 255, 255],\n",
            "        ...,\n",
            "        [254, 254, 254],\n",
            "        [254, 254, 254],\n",
            "        [254, 254, 254]],\n",
            "\n",
            "       [[255, 255, 255],\n",
            "        [255, 255, 255],\n",
            "        [255, 255, 255],\n",
            "        ...,\n",
            "        [254, 254, 254],\n",
            "        [254, 254, 254],\n",
            "        [254, 254, 254]]], dtype=uint8), boxes=array([[[ 71., 363.],\n",
            "        [419., 363.],\n",
            "        [419., 382.],\n",
            "        [ 71., 382.]],\n",
            "\n",
            "       [[ 71., 392.],\n",
            "        [410., 392.],\n",
            "        [410., 412.],\n",
            "        [ 71., 412.]],\n",
            "\n",
            "       [[185., 466.],\n",
            "        [317., 466.],\n",
            "        [317., 488.],\n",
            "        [185., 488.]]], dtype=float32), txts=('曲曲折折的荷塘上面，弥望的是田田的叶', '子。叶子出水很高，像亭亭的舞女的裙。', '—《荷塘月色》一'), scores=(0.99052, 0.99123, 0.89048), word_results=((('曲', 0.99946, [[71, 363], [86, 363], [86, 382], [71, 382]]), ('曲', 0.99871, [[89, 363], [105, 363], [105, 382], [89, 382]]), ('折', 0.99464, [[108, 363], [124, 363], [124, 382], [108, 382]]), ('折', 0.99041, [[131, 363], [146, 363], [146, 382], [131, 382]]), ('的', 0.99863, [[146, 363], [162, 363], [162, 382], [146, 382]]), ('荷', 0.9973, [[169, 363], [184, 363], [184, 382], [169, 382]]), ('塘', 0.99974, [[188, 363], [203, 363], [203, 382], [188, 382]]), ('上', 0.99861, [[207, 363], [222, 363], [222, 382], [207, 382]]), ('面', 0.9998, [[226, 363], [241, 363], [241, 382], [226, 382]]), ('，', 0.99184, [[241, 363], [257, 363], [257, 382], [241, 382]]), ('弥', 0.97042, [[263, 363], [279, 363], [279, 382], [263, 382]]), ('望', 0.99984, [[282, 363], [298, 363], [298, 382], [282, 382]]), ('的', 0.99935, [[305, 363], [320, 363], [320, 382], [305, 382]]), ('是', 0.99761, [[320, 363], [336, 363], [336, 382], [320, 382]]), ('田', 0.91549, [[343, 363], [358, 363], [358, 382], [343, 382]]), ('田', 0.98056, [[362, 363], [377, 363], [377, 382], [362, 382]]), ('的', 0.99915, [[381, 363], [396, 363], [396, 382], [381, 382]]), ('叶', 0.99785, [[400, 363], [415, 363], [415, 382], [400, 382]])), (('子', 0.97145, [[71, 392], [85, 392], [85, 412], [71, 412]]), ('。', 0.99549, [[85, 392], [100, 392], [100, 412], [85, 412]]), ('叶', 0.9971, [[107, 392], [124, 392], [124, 412], [107, 412]]), ('子', 0.9842, [[127, 392], [144, 392], [144, 412], [127, 412]]), ('出', 0.99946, [[147, 392], [164, 392], [164, 412], [147, 412]]), ('水', 0.99925, [[167, 392], [184, 392], [184, 412], [167, 412]]), ('很', 0.99851, [[187, 392], [204, 392], [204, 412], [187, 412]]), ('高', 0.99839, [[207, 392], [222, 392], [222, 412], [207, 412]]), ('，', 0.99541, [[222, 392], [237, 392], [237, 412], [222, 412]]), ('像', 0.99658, [[244, 392], [260, 392], [260, 412], [244, 412]]), ('亭', 0.99799, [[264, 392], [280, 392], [280, 412], [264, 412]]), ('亭', 0.99763, [[284, 392], [300, 392], [300, 412], [284, 412]]), ('的', 0.9994, [[304, 392], [320, 392], [320, 412], [304, 412]]), ('舞', 0.99999, [[324, 392], [340, 392], [340, 412], [324, 412]]), ('女', 0.99948, [[340, 392], [357, 392], [357, 412], [340, 412]]), ('的', 0.99899, [[360, 392], [377, 392], [377, 412], [360, 412]]), ('裙', 0.91906, [[380, 392], [397, 392], [397, 412], [380, 412]]), ('。', 0.99379, [[397, 392], [410, 392], [410, 412], [397, 412]])), (('—', 0.4499, [[185, 466], [199, 466], [199, 488], [185, 488]]), ('《', 0.99835, [[199, 466], [215, 466], [215, 488], [199, 488]]), ('荷', 0.99948, [[215, 466], [232, 466], [232, 488], [215, 488]]), ('塘', 0.99998, [[232, 466], [248, 466], [248, 488], [232, 488]]), ('月', 0.99904, [[248, 466], [265, 466], [265, 488], [248, 488]]), ('色', 0.99988, [[265, 466], [281, 466], [281, 488], [265, 488]]), ('》', 0.99965, [[281, 466], [298, 466], [298, 488], [281, 488]]), ('一', 0.67754, [[298, 466], [316, 466], [316, 488], [298, 488]]))), elapse_list=[0.4662188659999913, 0.0055959224700927734, 0.36265224000001695], elapse=0.834467028470101, lang_type=<LangRec.CH: 'ch'>)\n"
          ]
        },
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "\u001b[32m[INFO] 2025-06-19 13:02:01,302 [RapidOCR] download_file.py:78: Download size: 3.09MB\u001b[0m\n",
            "\u001b[32m[INFO] 2025-06-19 13:02:04,440 [RapidOCR] download_file.py:91: Successfully saved to: /usr/local/lib/python3.11/dist-packages/rapidocr/models/FZYTK.TTF\u001b[0m\n",
            "\u001b[32m[INFO] 2025-06-19 13:02:04,485 [RapidOCR] output.py:79: Single word visualization saved as vis_result.jpg\u001b[0m\n"
          ]
        },
        {
          "data": {
            "text/html": [
              "<style>\n",
              "      .ndarray_repr .ndarray_raw_data {\n",
              "        display: none;\n",
              "      }\n",
              "      .ndarray_repr.show_array .ndarray_raw_data {\n",
              "        display: block;\n",
              "      }\n",
              "      .ndarray_repr.show_array .ndarray_image_preview {\n",
              "        display: none;\n",
              "      }\n",
              "      </style>\n",
              "      <div id=\"id-2369dc9f-eace-4508-ad5f-86ab6b62c353\" class=\"ndarray_repr\"><pre>ndarray (516, 1000, 3) <button style=\"padding: 0 2px;\">show data</button></pre><img src=\"\" class=\"ndarray_image_preview\" /><pre class=\"ndarray_raw_data\">array([[[255, 255, 255],\n",
              "        [255, 255, 255],\n",
              "        [255, 255, 255],\n",
              "        ...,\n",
              "        [255, 255, 255],\n",
              "        [255, 255, 255],\n",
              "        [255, 255, 255]],\n",
              "\n",
              "       [[255, 255, 255],\n",
              "        [255, 255, 255],\n",
              "        [255, 255, 255],\n",
              "        ...,\n",
              "        [255, 255, 255],\n",
              "        [255, 255, 255],\n",
              "        [255, 255, 255]],\n",
              "\n",
              "       [[255, 255, 255],\n",
              "        [255, 255, 255],\n",
              "        [255, 255, 255],\n",
              "        ...,\n",
              "        [255, 255, 255],\n",
              "        [255, 255, 255],\n",
              "        [255, 255, 255]],\n",
              "\n",
              "       ...,\n",
              "\n",
              "       [[255, 255, 255],\n",
              "        [255, 255, 255],\n",
              "        [255, 255, 255],\n",
              "        ...,\n",
              "        [255, 255, 255],\n",
              "        [255, 255, 255],\n",
              "        [255, 255, 255]],\n",
              "\n",
              "       [[255, 255, 255],\n",
              "        [255, 255, 255],\n",
              "        [255, 255, 255],\n",
              "        ...,\n",
              "        [255, 255, 255],\n",
              "        [255, 255, 255],\n",
              "        [255, 255, 255]],\n",
              "\n",
              "       [[255, 255, 255],\n",
              "        [255, 255, 255],\n",
              "        [255, 255, 255],\n",
              "        ...,\n",
              "        [255, 255, 255],\n",
              "        [255, 255, 255],\n",
              "        [255, 255, 255]]], dtype=uint8)</pre></div><script>\n",
              "      (() => {\n",
              "      const titles = ['show data', 'hide data'];\n",
              "      let index = 0\n",
              "      document.querySelector('#id-2369dc9f-eace-4508-ad5f-86ab6b62c353 button').onclick = (e) => {\n",
              "        document.querySelector('#id-2369dc9f-eace-4508-ad5f-86ab6b62c353').classList.toggle('show_array');\n",
              "        index = (++index) % 2;\n",
              "        document.querySelector('#id-2369dc9f-eace-4508-ad5f-86ab6b62c353 button').textContent = titles[index];\n",
              "        e.preventDefault();\n",
              "        e.stopPropagation();\n",
              "      }\n",
              "      })();\n",
              "    </script>"
            ],
            "text/plain": [
              "array([[[255, 255, 255],\n",
              "        [255, 255, 255],\n",
              "        [255, 255, 255],\n",
              "        ...,\n",
              "        [255, 255, 255],\n",
              "        [255, 255, 255],\n",
              "        [255, 255, 255]],\n",
              "\n",
              "       [[255, 255, 255],\n",
              "        [255, 255, 255],\n",
              "        [255, 255, 255],\n",
              "        ...,\n",
              "        [255, 255, 255],\n",
              "        [255, 255, 255],\n",
              "        [255, 255, 255]],\n",
              "\n",
              "       [[255, 255, 255],\n",
              "        [255, 255, 255],\n",
              "        [255, 255, 255],\n",
              "        ...,\n",
              "        [255, 255, 255],\n",
              "        [255, 255, 255],\n",
              "        [255, 255, 255]],\n",
              "\n",
              "       ...,\n",
              "\n",
              "       [[255, 255, 255],\n",
              "        [255, 255, 255],\n",
              "        [255, 255, 255],\n",
              "        ...,\n",
              "        [255, 255, 255],\n",
              "        [255, 255, 255],\n",
              "        [255, 255, 255]],\n",
              "\n",
              "       [[255, 255, 255],\n",
              "        [255, 255, 255],\n",
              "        [255, 255, 255],\n",
              "        ...,\n",
              "        [255, 255, 255],\n",
              "        [255, 255, 255],\n",
              "        [255, 255, 255]],\n",
              "\n",
              "       [[255, 255, 255],\n",
              "        [255, 255, 255],\n",
              "        [255, 255, 255],\n",
              "        ...,\n",
              "        [255, 255, 255],\n",
              "        [255, 255, 255],\n",
              "        [255, 255, 255]]], dtype=uint8)"
            ]
          },
          "execution_count": 2,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "from rapidocr import RapidOCR\n",
        "\n",
        "engine = RapidOCR()\n",
        "\n",
        "img_url = \"https://img1.baidu.com/it/u=3619974146,1266987475&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=516\"\n",
        "result = engine(img_url, return_word_box=True, return_single_char_box=True)\n",
        "print(result)\n",
        "\n",
        "result.vis(\"vis_result.jpg\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "nzSxM43V9rAL"
      },
      "outputs": [],
      "source": []
    }
  ],
  "metadata": {
    "colab": {
      "provenance": [],
      "toc_visible": true
    },
    "kernelspec": {
      "display_name": "demo",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.13"
    },
    "vscode": {
      "interpreter": {
        "hash": "d42ac74e361e91c495bc3239dc10e7028f0ad770b2a1f806c814d17578354ad7"
      }
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
